<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-100px, -100px);
            width: 200px;
            height: 200px;
            background-color: #6cf;
        }
    </style>
</head>

<body>
    <div></div>
</body>
<script>
    var dv = document.querySelector('div');
    var kuan = window.innerWidth;
    var gao = window.innerHeight;
    var x = y = -100
    document.addEventListener('keydown', function (e) {
        if (e.keyCode == 87) {
            if (y == -gao / 2) {
                return false
            } else {
                dv.style.transform = `translate(${x}px,${y -= 1}px)`
            }
        } else if (e.keyCode == 65) {
            if (x == -kuan / 2) {
                return false
            } else {
                dv.style.transform = `translate(${x -= 1}px,${y}px)`
            }
        } else if (e.keyCode == 83) {
            if (y == gao / 2 - 200) {
                return false
            } else {
                dv.style.transform = `translate(${x}px,${y += 1}px)`
            }
        } else if (e.keyCode == 68) {
            if (x == kuan / 2 - 200) {
                return false
            } else {
                dv.style.transform = `translate(${x += 1}px,${y}px)`
            }
        }
    })
</script>

</html>